package com.example.neusoft215gdh.androidpro_21.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft215gdh.androidpro_21.Common.utilMeth;
import com.example.neusoft215gdh.androidpro_21.JavaBean.StringBean;
import com.example.neusoft215gdh.androidpro_21.Listener.UserListener;
import com.example.neusoft215gdh.androidpro_21.Model.UserModel;
import com.example.neusoft215gdh.androidpro_21.R;

public class LoginActivity extends AppCompatActivity implements utilMeth,View.OnClickListener,UserListener {

    private Button login;
    private TextView regUser;
    private Button loginUser;
    public EditText username;
    public EditText password;
    String uname;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        inEvents();
        login.setOnClickListener(this);
    }

    public void initViews() {
        regUser = (TextView) findViewById(R.id.regUser);
        loginUser = (Button) findViewById(R.id.login);
        username = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.password);
        login= (Button) findViewById(R.id.login);

    }

    public void inEvents() {
        regUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(com.example.neusoft215gdh.androidpro_21.Activity.LoginActivity.this, com.example.neusoft215gdh.androidpro_21.Activity.RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
            uname=username.getText().toString();
                savaUser(uname);
                pwd=password.getText().toString();
                UserModel userModel=new UserModel();
                userModel.getLogin(uname,pwd,this);
                break;
        }
    }

    @Override
    public void onResponse(StringBean stringBean) {

        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
//        savaUser(stringBean.getId());//保存用户id
         savaUser(stringBean.getId());
        Intent intent=new Intent(com.example.neusoft215gdh.androidpro_21.Activity.LoginActivity.this, com.example.neusoft215gdh.androidpro_21.Activity.MainActivity.class);
        startActivity(intent);
//        finish();
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, ""+msg, Toast.LENGTH_SHORT).show();
    }

public  void savaUser(String userid){
    SharedPreferences sharedPreferences=getSharedPreferences("Userid", Context.MODE_PRIVATE);
    SharedPreferences.Editor edit=sharedPreferences.edit();
    edit.putString("user_id",userid);
    edit.commit();
}

}
